package com.strato.mvc.view.velocity.v1_0_0;

import java.io.IOException;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.exception.MethodInvocationException;
import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.exception.ResourceNotFoundException;

import com.strato.mvc.api.v1_0_0.view.View;

public class VelocityView implements View {

	private Template template;
	
	@Override
	public void render(HttpServletRequest request, HttpServletResponse response, Map<?, ?> model) {
		response.setContentType("text/html;charset=UTF-8");
		VelocityContext context=new VelocityContext(model);
		context.put("request", request);
		context.put("response", response);
		try {
			template.merge(context, response.getWriter());
		} catch (ResourceNotFoundException e) {
			throw new RuntimeException(e);
		} catch (ParseErrorException e) {
			throw new RuntimeException(e);
		} catch (MethodInvocationException e) {
			throw new RuntimeException(e);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	public Template getTemplate() {
		return template;
	}

	public void setTemplate(Template template) {
		this.template = template;
	}

}
